/*
 * @Descripttion:
 * @Author: qinyonglian
 * @Date: 2020-09-23 15:00:50
 * @LastEditors: qinyonglian
 * @LastEditTime: 2020-09-24 16:16:08
 */
const watermark = {};
const curDate = new Date();
const curTime = `${curDate.getFullYear()}-${curDate.getMonth()}-${curDate.getDate()}  ${curDate.getHours()}:${curDate.getMinutes()}`;
const setWatermark = (parent, str, str2) => {
  const id = '1.23452384164.123412415';
  if (document.getElementById(id) !== null) {
    // document.body.removeChild(document.getElementById(id));
    document.getElementById(id).parentNode.removeChild(document.getElementById(id));
  }

  const can = document.createElement('canvas');
  can.width = 190;
  can.height = 115;

  const cans = can.getContext('2d');
  cans.rotate(-15 * Math.PI / 180);
  cans.font = '15px Vedana';
  cans.fillStyle = 'rgba(200, 200, 200, 0.20)';
  cans.textAlign = 'center';
  cans.textBaseline = 'Middle';
  // cans.fillText(str, can.width / 3, can.height / 2);
  cans.fillText(str, 50, 50); // 第一行字体
  cans.fillText(str2, 50, 65); // 第二行字体

  const div = document.createElement('div');
  div.id = id;
  div.style.pointerEvents = 'none';
  div.style.top = '0';
  div.style.left = '0';
  div.style.position = 'absolute';
  div.style.zIndex = '100000';
  div.style.width = '100%';
  div.style.height = '100%';
  div.style.background = `url(${can.toDataURL('image/png')}) left top repeat`;
  document.getElementById(parent) && document.getElementById(parent).appendChild(div);
  return id;
};

// 该方法只允许调用一次
watermark.set = (parent) => {
  let id = setWatermark(parent, window.parent.JE.currentUser.username, curTime);
  setInterval(() => {
    if (document.getElementById(id) === null) {
      id = setWatermark(parent, window.parent.JE.currentUser.username, curTime);
    }
  }, 500);
  window.onresize = () => {
    setWatermark(parent, window.parent.JE.currentUser.username, curTime);
  };
};
export default watermark;
